# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import scrapy
import openpyxl

class Demo2Pipeline:
    def process_item(self, item, spider):
        return item

class csvPipeline:
    def __init__(self):
        self.wb = openpyxl.Workbook()
        self.ws = None

    def process_item(self, item, spider):
        if self.ws is None:
            self.ws = self.wb.active
            fieldname = list(item.keys())
            self.ws.title = spider.name
            self.ws.append(fieldname)
        self.ws.append(list(item.values()))
        return item

    def close_spider(self,spider):
        self.wb.save(f'{self.ws.title}.csv')
